//Page Object
const app = getApp();
Page({
    data: {
        imageUrl: 'https://armeco.happydoit.com/upload', // 服务器图片路径
        cityList: [], // 城市列表
        showCityList: false, // 是否展示城市列表
        chooseCityName: '', // 选择的城市名称 string类型
        chooseCityId: -1, // 选择的城市id number类型
        chooseNum: 0, // 选择的城市数量 number类型
        cityName: '', // 确认的城市名称 string类型
        cityId: -1, // 确认的城市id number类型
        cityNum: 0, // 确认的城市数量 number类型
        sureHelp: false, // 确认助力城市弹窗
        successHelp: false, // 成功助力弹窗
        myHelp: false, // 我的助力为0
        stage: 1, // 城市阶段
        gotOut: false, // 是否违规
    },

    onLoad: function () {
        let that = this;
        that.setData({
            canFocus: app.globalData.canFocus
        })
    },
    onReady: function () {

    },
    onShow: function () {
        this.isGetOut();
    },
    onHide: function () {

    },
    onUnload: function () {

    },
    onPullDownRefresh: function () {

    },
    onReachBottom: function () {

    },
    onShareAppMessage: function () {
        let that = this;
        return {
            title: '让邓伦来我的芯愿城市，就靠你的助力了！',
            path: '/pages/stage/cityHelp/cityHelp',
            imageUrl: that.data.imageUrl + '/cityimages/share-img.png'
          }
    },
    onPageScroll: function () {

    },

    // 授权
    bindGetUserInfo: function (e) {
        let that = this;
        if (e.detail.errMsg == 'getUserInfo:ok') {
            wx.request({
                url: app.globalData.requestUrl + '/api/recipeInfo',
                data: {
                    nickname: e.detail.userInfo.nickName
                },
                method: "POST",
                header: {
                    "Content-Type": "application/x-www-form-urlencoded",
                    "Cookie": "JSESSIONID=" + app.globalData.jessionId
                },
                complete: function () {
                    that.showCityList();
                }
            })
        }
    },

    // 展示城市列表
    showCityList: function () {
        let that = this;
        wx.showLoading({
            title: '加载中',
            mask: true,
        });
        wx.request({
            url: app.globalData.requestUrl + '/api/city/citylist',
            data: {},
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Cookie": "JSESSIONID=" + app.globalData.jessionId
            },
            success: function (res) {
                if (res.data.code == 'success') {
                    wx.hideLoading();
                    that.setData({
                        cityList: res.data.data.citylist,
                        stage: res.data.data.stage
                    })
                } else {
                    wx.hideLoading();
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        mask: true
                    })
                }
            }
        })
        this.setData({
            showCityList: true
        })
    },

    // 选择城市
    selectCity: function (e) {
        let that = this;
        let id = e.currentTarget.dataset.id;
        let name = e.currentTarget.dataset.name;
        let number = e.currentTarget.dataset.num;
        that.setData({
            chooseCityName: name,
            chooseCityId: id,
            chooseNum: number
        })
    },

    // 确认城市
    sureCity: function () {
        let that = this;
        let chooseCityName = that.data.chooseCityName;
        let chooseCityId = that.data.chooseCityId;
        if (chooseCityName === '' || chooseCityId === -1) {
            wx.showToast({
                title: '请选择城市~',
                icon: 'none',
                mask: true,
            });
            return;
        };
        that.setData({
            showCityList: false,
            cityName: chooseCityName,
            cityId: chooseCityId
        })
    },

    // 确认助力城市弹窗
    showHelp: function () {
        let that = this;
        if (that.data.cityId === -1) {
            wx.showToast({
                title: '请选择城市~',
                icon: 'none',
                mask: true,
            })
            return;
        }
        that.setData({
            sureHelp: true
        })
    },

    // 关闭助力城市弹窗
    closeHelp: function () {
        this.setData({
            sureHelp: false,
            successHelp: false,
            myHelp: false
        })
    },

    // 确认助力城市
    sureHelp: function () {
        let that = this;
        wx.showLoading({
            title: '加载中',
            mask: true,
        });
        let data = {
            cityid: that.data.cityId
          }
        wx.request({
            url: app.globalData.requestUrl + '/api/city/assistanceCity',
            data: data,
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Cookie": "JSESSIONID=" + app.globalData.jessionId
            },
            success: function (res) {
                if (res.data.code == 'success') {
                    wx.hideLoading();
                    that.setData({
                        successHelp: true
                    })
                } else {
                    wx.hideLoading();
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        mask: true
                    })
                }
            }
        })
    },

    // 我的助力
    myHelp: function () {
        let that = this;
        if (that.data.cityName == '' || !that.data.cityName) {
            wx.showToast({
                title: '请选择芯愿城市~',
                icon: 'none',
                mask: true
            })
            return;
        }
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        wx.navigateTo({
            url: '/pages/stage/myHelp/myHelp?cityName=' + that.data.cityName + '&&cityId=' + that.data.cityId,
            success: function () {
                wx.hideLoading();
            }
        });
    },

    // 跳转我的助力页面
    toMyHelp: function () {
        this.setData({
            sureHelp: false,
            successHelp: false,
            myHelp: false
        });
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        wx.navigateTo({
            url: '/pages/stage/myHelp/myHelp?cityName=' + that.data.cityName + '&&cityId=' + that.data.cityId,
            success: function () {
                wx.hideLoading();
            }
        });
    },

    // 获取城市排行
    getCityRank: function () {
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        wx.navigateTo({
            url: '/pages/stage/rank/rank',
            success: function () {
                wx.hideLoading();
            }
        })
    },

    showFocus() {
        this.setData({
            isFocus: true
        })
    },

    hideFocus() {
        this.setData({
            isFocus: false
        })
    },

    openRule() {
        this.setData({
          showRule: true
        })
      },
    
    onGetCode: function (e) {
        this.setData({
            showRule: e.detail.val
        })
    },

    // 查询是否违规
    isGetOut: function () {
        let that = this;
        wx.showLoading({
            title: '加载中',
            mask: true,
        });
        wx.request({
            url: app.globalData.requestUrl + '/api/city/resetFalseCouldUseCups',
            data: {},
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Cookie": "JSESSIONID=" + app.globalData.jessionId
            },
            success: function (res) {
                if (res.data.code == 'success') {
                    wx.hideLoading();
                    that.setData({
                        gotOut: true,
                        gotOutMsg: res.data.msg
                    })
                } else {
                    wx.hideLoading();
                    if (!res.data.msg || res.data.msg == '') {
                        return;
                    } else {
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'none',
                            mask: true
                        })
                    }
                }
            }
        })
    },

    closeOut: function () {
        this.setData({
            gotOut: false
        })
    }
});